#region System
using System;
using System.Collections.Generic;
using System.IO;
using CF.Framework.Engine.Drawing;
using CF.Framework.Engine.Misc;
using CF.Framework.Engine.Screens;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
#endregion

namespace CF.Framework.Engine.Effects
{
	public class ScaleEffect : Effect
	{
		private float scaleValue;
		private float step;
		private float doFrom;
		private float doTo;
		private bool plus;

		public float From { get; set; }
		public float To { get; set; }
		public TimeSpan Duration { get; set; }
		public bool IsLoop { get; set; }

		public ScaleEffect()
		{
			From = 1;
			To = 2;
			Duration = TimeSpan.FromSeconds(1);
			IsLoop = false;
		}

		public override void OnStart()
		{
			doFrom = From;
			doTo = To;

			step = (float)(Math.Abs(doFrom - doTo) / (Duration.TotalSeconds * GameApp.Current.InitialFps));

			if (doFrom > doTo)
			{
				step = 0 - step;
				plus = false;
			}
			else
			{
				plus = true;
			}

			scaleValue = doFrom;
		}

		public override void OnStop()
		{
		}

		public override void OnUpdate(GameTime gameTime)
		{
			if (!IsRunning)
			{
				return;
			}

			if (IsDoing
				&& Target != null)
			{
				scaleValue += step;

				if ((plus && scaleValue >= doTo)
					|| (!plus && scaleValue < doTo))
				{
					scaleValue = doTo;

					if (this.IsLoop)
					{
						plus = !plus;
						step = 0 - step;

						float temp = doTo;

						doTo = doFrom;
						doFrom = temp;
					}
					else
					{
						IsCompleted = true;
						IsDoing = false;
						IsRunning = false;
					}
				}

				Target.Scale = scaleValue;
			}
		}
	}
}
